﻿using System;
using System.Collections.Generic;
using System.Data;
using XuanFe.AppLaunch.Common;
using XuanFe.Database.BLL;
using XuanFe.Database.DAL;
using XuanFe.Database.Model;

namespace XuanFe.AppLaunch
{
    public partial class StaffLogForm : DevExpress.XtraEditors.XtraForm
    {
        private const string m_ModuleId = "0104";

        private int colIndexLOG_ID = 0;
        private int colIndexSTAFF_NAME = 0;
        private int colIndexOPERATE_TIME = 0;
        private int colIndexMODULE_NAME = 0;
        private int colIndexREMARK = 0;

        private List<StaffModel> m_Staffs = new List<StaffModel>();

        public StaffLogForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 清空记录
        /// </summary>
        private void ClearRecordToGrid()
        {
            this.gridView1.BeginUpdate();
            this.gridView1.Columns.Clear();

            DevExpress.XtraGrid.Columns.GridColumn c;
            int visibleIndex = 0;

            this.colIndexLOG_ID = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "LOG_ID";
            c.Caption = "日志编码";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "STAFF_ID", "总数")});
            this.gridView1.Columns.Add(c);

            this.colIndexSTAFF_NAME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "STAFF_NAME";
            c.Caption = "姓名";
            c.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            c.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
            new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Count)});
            this.gridView1.Columns.Add(c);

            this.colIndexOPERATE_TIME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "OPERATE_TIME";
            c.Caption = "操作时间";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexMODULE_NAME = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "MODULE_NAME";
            c.Caption = "模块";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.colIndexREMARK = this.gridView1.Columns.Count;
            c = new DevExpress.XtraGrid.Columns.GridColumn();
            c.FieldName = "REMARK";
            c.Caption = "备注";
            c.OptionsColumn.ReadOnly = true;
            c.OptionsColumn.AllowFocus = false;
            c.Visible = true;
            c.VisibleIndex = visibleIndex++;
            this.gridView1.Columns.Add(c);

            this.gridView1.EndUpdate();
        }

        /// <summary>
        /// 加载记录
        /// </summary>
        private void LoadRecordToGrid(QueryLogModel model)
        {
            using (DataSet ds = StaffLogDAL.Instance.GetDataSet(model))
            {
                this.gridControl1.DataSource = ds.Tables[0];
                this.gridView1.BestFitColumns();
            }
        }
        private void BuildAll()
        {
            this.m_Staffs = StaffDAL.Instance.Gets(true);

            this.cmbStaff.Properties.Items.Clear();
            this.cmbStaff.Properties.Items.AddRange(this.m_Staffs);
        }

        private void StaffLogForm_Load(object sender, EventArgs e)
        {
            try
            {
                DateTime now = DateTime.Now;
                this.dteStartTime.DateTime = now.Date;
                this.dteEndTime.DateTime = now.Date.AddDays(1).AddSeconds(-1);

                this.BuildAll();
                this.ClearRecordToGrid();

                StaffLogBLL.Instance.InsertLog(LoginInfo.Instance.StaffLogon.Staff.Id, m_ModuleId, "浏览记录");
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                QueryLogModel model = new QueryLogModel() { QueryOperateTime = true, StartTime = this.dteStartTime.DateTime, EndTime = this.dteEndTime.DateTime };
                if (this.cmbStaff.SelectedIndex != -1 && this.cmbStaff.SelectedItem != null)
                {
                    model.QueryStaffId = true;
                    model.StaffId = (this.cmbStaff.SelectedItem as StaffModel).Id;
                }
                if (!string.IsNullOrWhiteSpace(this.txtKey.Text.Trim()))
                {
                    model.QueryKeyword = true;
                    model.Keyword = this.txtKey.Text.Trim();
                }
                this.LoadRecordToGrid(model);
            }
            catch (Exception ex)
            {
                Global.Instance.ShowExceptionDlg(ex);
            }
        }
    }
}